<?php


namespace App\Http\Middleware;


use App\Utils\Output;
use Illuminate\Support\Facades\Route;

class AdminPermission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        list($controller, $action) = explode('@', Route::currentRouteAction());
        $preg = '#App\\\Http\\\Controllers\\\(.*?)Controller#is';
        $match = '';
        \preg_match_all($preg, $controller, $match);
        $controller = $match[1][0];
        $request->offsetSet('controller',$controller);
        if (!\App\Utils\Auth::canAuth($action,$controller)) {
            if ($request->ajax()) {
                return Output::error(-1, '没有权限');
            } else {
                return response('没有权限', 401);
            }
        }
        return $next($request);
    }
}
